package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhihu-passion/internal/models/dao"
	"zhihu-passion/internal/models/services"
	"zhihu-passion/internal/request"
)

func AddArticle(c *gin.Context) {
	var req request.Article
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	article := dao.Article{
		Title:   req.Title,
		Content: req.Content,
	}
	err = services.CreateArticle(article)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "发布文章成功",
		})
	}
}

func Browse(c *gin.Context) {
	var req request.Browse
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	article, err := services.BrowseArticle(req.Title)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "浏览失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "浏览成功",
			"data": article,
		})
	}
}

func AddGood(c *gin.Context) {
	var req request.Good
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	good := dao.Good{
		ArticleId: req.ArticleId,
		Num:       req.Num,
	}
	err = services.CreateGood(good)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "点赞失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "点赞成功",
			"data": gin.H{
				"ArticleId": req.ArticleId,
				"Num":       req.Num,
			},
		})
	}
}

func AddDiscuss(c *gin.Context) {
	var req request.Discuss
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	discuss := dao.Discuss{
		ArticleId: req.ArticleId,
		Content:   req.Content,
		Num:       req.Num,
	}
	err = services.CommentArticle(discuss)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "评论失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "评论成功",
		})
	}
}

func Reply(c *gin.Context) {
	var req request.Reply
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	reply := dao.Reply{
		FatherId: req.FatherId,
		Content:  req.Content,
		Num:      req.Num,
	}
	err = services.AddReply(reply)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "评论失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "评论成功",
		})
	}
}

func List(c *gin.Context) {
	var req request.List
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	list, err := services.GoodsList(req.Content)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "列表展示失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "列表展示成功",
			"data": list,
		})
	}
}

func Statistics(c *gin.Context) {
	var req request.Statistics
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	statistics := dao.Statistics{
		ArticleId: req.ArticleId,
		Content:   req.Content,
		Num:       req.Num,
	}
	err = services.AddStatistics(statistics)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "评论失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "评论成功",
		})
	}
}

func Del(c *gin.Context) {
	var req request.Del
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	err = services.Del(req.ArticleId)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "删除成功",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "删除成功",
		})
	}
}
